home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Screenblankers / GBlanker / GSource / Blankers / Scrawls / blank.c next >
C/C++ Source or Header  |  1996-09-26  |  6KB  |  291 lines

  1. /*
  2. **            Scrawls Blanker
  3. **
  4. **        Copyright © 1995 by Marzio De Biasi
  5. **
  6. **
  7. **    Source:        blank.c
  8. **    Language:    ANSI C
  9. **    Version:    1.0
  10. **    Description:    a module for GarshneBlanker v38.8 modular screen
  11. **            blanker by Micheal D. Bayne, see Scrawls.doc for
  12. **            further details.
  13. **    Last update:    24 Jan 95        
  14. **    Author:        De Biasi Marzio
  15. **            Address: via Borgo Simoi, 34
  16. **                 31029 Vittorio Veneto (TV)
  17. **                 Italy
  18. **            E-Mail:  debiasi@dimi.uniud.it
  19. **
  20. **    History:    
  21. **
  22. **    ->
  23. **
  24. */
  25.  
  26. #include "/includes.h"
  27.  
  28. #include <math.h>
  29. #include <time.h>
  30.  
  31. #define PREF_EQU    0
  32. #define PREF_FAC     2
  33. #define PREF_ITER     4
  34. #define PREF_ZOOM    6
  35. #define PREF_DELAY    8
  36. #define MODE        10
  37.  
  38. LONG Hei, Wid;
  39. Triplet *ColorTable = 0L;
  40. WORD Cl;
  41.  
  42.  
  43.  
  44. struct Screen     *pubIFS_sc;
  45. struct RastPort *pubIFS_rp;
  46.  
  47.  
  48. #define MAX_IFS_ITER    20
  49.  
  50. struct    IFS_eq {
  51.  
  52.     float    par[6];
  53.     WORD    prob;
  54. };
  55.  
  56. typedef struct IFS_eq IFS_eq_t;
  57.  
  58. LONG        pubIFS_delay = 0;
  59. LONG        pubIFS_iter = 8000;
  60. IFS_eq_t    pubIFS_eq[MAX_IFS_ITER];
  61. LONG        pubIFS_numeq = 4;
  62. float        pubIFS_fac, pubIFS_zoom;
  63.  
  64. #include "Scrawls_rev.h"
  65. STATIC const UBYTE VersTag[] = VERSTAG;
  66.  
  67.  
  68.  
  69. VOID Defaults( PrefObject *Prefs )
  70. {
  71.     Prefs[PREF_EQU].po_Level = 4;
  72.     Prefs[PREF_FAC].po_Level = 4;
  73.     Prefs[PREF_ITER].po_Level = 80;
  74.     Prefs[PREF_ZOOM].po_Level = 20;
  75.     Prefs[PREF_DELAY].po_Level = 0;
  76.     Prefs[MODE].po_ModeID = getTopScreenMode();
  77.     Prefs[MODE].po_Depth = 4;
  78. }
  79.  
  80.  
  81.  
  82.  
  83. /* ------------------------------------------------------------------- */
  84. /* ---- SetUpField() ------------------------------------------------- */
  85. /* ------------------------------------------------------------------- */
  86.  
  87. void    SetUpField(void) {
  88. /*
  89. **  Clears screen, and builds the iterated function system.
  90. */
  91.  
  92.     WORD    i, j, t_prob;
  93.  
  94.     SetRast( pubIFS_rp, 0L );
  95.     ScreenToFront(pubIFS_sc);
  96.  
  97.     t_prob = 100 - pubIFS_numeq + 1;
  98.  
  99.     for (j = 0; j < pubIFS_numeq; j++) {
  100.         for (i = 0; i < 6; i++) {
  101.         pubIFS_eq[j].par[i] = (drand48() * 2.0) - 1.;
  102.         if ((i == 2) || (i == 5)) pubIFS_eq[j].par[i] *= pubIFS_fac;
  103.     }    
  104.  
  105.     if (j < (pubIFS_numeq - 1)) {
  106.         if (t_prob > 0) pubIFS_eq[j].prob = lrand48() % t_prob + 1;
  107.         else pubIFS_eq[j].prob = 1;
  108.  
  109.         t_prob = t_prob - pubIFS_eq[j].prob + 1;
  110.     }
  111.     else {
  112.         pubIFS_eq[j].prob = t_prob;
  113.     }
  114.     }
  115. }
  116.  
  117.  
  118.  
  119.  
  120. /* ------------------------------------------------------------------- */
  121. /* ---- SetPixel() --------------------------------------------------- */
  122. /* ------------------------------------------------------------------- */
  123.  
  124. BOOL    SetPixel(float x, float y) {
  125. /*
  126. **  Writes a pixel.
  127. */
  128.  
  129.     WORD    px, py, c;
  130.  
  131.     px = (x * pubIFS_zoom + (Wid / 2)) + 0.5;
  132.     py = (y * pubIFS_zoom + (Hei / 2)) + 0.5;
  133.  
  134.     if ((px < 0) || (px >= Wid) || (py < 0) || (py >Hei)) {
  135.     return(FALSE);
  136.     } 
  137.     else {
  138.  
  139.     c = ReadPixel(pubIFS_rp, px, py);
  140.     c = (c < (Cl - 1))? c + 1 : 0;
  141.     SetAPen(pubIFS_rp, c);
  142.     WritePixel(pubIFS_rp, px, py);
  143.     return(TRUE);
  144.     }
  145. }
  146.  
  147.  
  148.  
  149.  
  150. /* ------------------------------------------------------------------- */
  151. /* ---- DoIFS() ------------------------------------------------------ */
  152. /* ------------------------------------------------------------------- */
  153.  
  154. void    DoIFS(float *x, float *y) {
  155. /*
  156. **  Randomly chooses a function, then apply it to point (<x>,<y>).
  157. */
  158.  
  159.     float    t_x, t_y;
  160.     WORD    n, eq;
  161.  
  162.     n = lrand48() % 100 + 1;
  163.     eq = 0;
  164.  
  165.     while ( (n = n - pubIFS_eq[eq].prob) > 0) eq++; 
  166.     if (eq >= pubIFS_numeq) eq = pubIFS_numeq - 1;
  167.  
  168.     t_x = pubIFS_eq[eq].par[0] * (*x) + pubIFS_eq[eq].par[1] * (*y) + \
  169.       pubIFS_eq[eq].par[2];
  170.     t_y = pubIFS_eq[eq].par[3] * (*x) + pubIFS_eq[eq].par[4] * (*y) + \
  171.       pubIFS_eq[eq].par[5];
  172.  
  173.     *x = t_x;
  174.     *y = t_y;
  175. }
  176.  
  177.  
  178.  
  179.  
  180. /* ------------------------------------------------------------------- */
  181. /* ---- IFSDriver() -------------------------------------------------- */
  182. /* ------------------------------------------------------------------- */
  183.  
  184. LONG    IFSDriver( void ) {
  185. /*
  186. **  Main loop: chooses the starting point, transforms it several times, then
  187. **  starts calculating the attractor of the IFS.
  188. */
  189.  
  190.     LONG    RetVal = OK;
  191.     LONG    delay_count, iter_count;
  192.     WORD    i;
  193.     UBYTE    skip_flag;
  194.     LONG    scr2front = 0;
  195.     float    x, y;
  196.  
  197.  
  198.     while (RetVal == OK) {
  199.  
  200.         ColorTable = RainbowPalette( pubIFS_sc, 0L, 1L, 0L );
  201.         SetUpField();
  202.  
  203.  
  204.     x = drand48() * (Wid / 2.0);
  205.     y = drand48() * (Hei / 2.0);
  206.  
  207.         delay_count = 0;
  208.     iter_count = 0;
  209.     skip_flag = FALSE;
  210.  
  211.     for (i = 0; i < 20; i++)  DoIFS(&x,&y);
  212.  
  213.     while (! skip_flag) {
  214.  
  215.         if ((scr2front++ % 50) == 0) ScreenToFront(pubIFS_sc);
  216.  
  217.         for (i = 0; i < pubIFS_delay; i++) WaitTOF();
  218.  
  219.         DoIFS(&x, &y);
  220.         SetPixel(x,y);
  221.  
  222.         if (iter_count++ > pubIFS_iter) skip_flag = TRUE;
  223.  
  224.         if ((RetVal = ContinueBlanking()) != OK) skip_flag = TRUE;
  225.     }
  226.  
  227.  
  228.     RainbowPalette( 0L, ColorTable, 1L, 0L );
  229.     }
  230.  
  231.     return(RetVal);
  232. }
  233.  
  234.  
  235.  
  236.  
  237. /* ------------------------------------------------------------------- */
  238. /* ---- Blank() ------------------------------------------------------ */
  239. /* ------------------------------------------------------------------- */
  240.  
  241. LONG Blank( PrefObject *Prefs )
  242. {
  243. /*
  244. **  Opens screen, initializes color table, preference values and random
  245. **  numbers generator seed, then call <IFSDriver()>.
  246. */
  247.     struct Window *Wnd;
  248.     LONG  RetVal;
  249.  
  250.     srand48(time(0L));
  251.  
  252.     pubIFS_sc = OpenScreenTags( NULL, SA_Depth, Prefs[MODE].po_Depth, SA_Quiet, TRUE,
  253.                          SA_DisplayID, Prefs[MODE].po_ModeID, SA_Behind, TRUE,
  254.                          SA_Overscan, OSCAN_STANDARD, TAG_DONE );
  255.  
  256.     if( pubIFS_sc )
  257.     {
  258.         pubIFS_rp = &pubIFS_sc->RastPort;
  259.  
  260.         Cl = 1L << Prefs[MODE].po_Depth;
  261.  
  262.  
  263.         Wid = pubIFS_sc->Width;
  264.         Hei = pubIFS_sc->Height;
  265.  
  266.             ColorTable = RainbowPalette( pubIFS_sc, 0L, 1L, 0L );
  267. /*        SetRGB4(&(pubIFS_sc->ViewPort),1,0xF,0xF,0xF);
  268. */        
  269.  
  270.  
  271.         Wnd = BlankMousePointer( pubIFS_sc );
  272.         
  273.     
  274.         pubIFS_numeq = Prefs[PREF_EQU].po_Level;
  275.         pubIFS_fac = (float)Prefs[PREF_FAC].po_Level;
  276.         pubIFS_iter = Prefs[PREF_ITER].po_Level * 100;
  277.         pubIFS_zoom = (float)Prefs[PREF_ZOOM].po_Level;
  278.         pubIFS_delay = Prefs[PREF_DELAY].po_Level;
  279.  
  280.         RetVal = IFSDriver();
  281.  
  282.         UnblankMousePointer( Wnd );
  283.  
  284.         CloseScreen( pubIFS_sc );
  285.     }
  286.     else
  287.         RetVal = FAILED;
  288.     
  289.     return(RetVal);
  290. }
  291.